package com.tz.service;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




@Service
public class ProducerService {
    private static final Logger log = LoggerFactory.getLogger(ProducerService.class);
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    public void sendMessage(String userId,String content) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", userId);
        jsonObject.put("content", content);
        rocketMQTemplate.convertAndSend("my-topic", jsonObject.toJSONString());
//        Message<String> msg = MessageBuilder.withPayload("Hello,RocketMQ")
//                //.setHeader(MessageConst.PROPERTY_TAGS, "自定义tag")
//                .build();
//        SendResult sendResult = rocketMQTemplate.send("my-topic", msg);
//        rocketMQTemplate.se
        //rocketMQTemplate.sendAndReceive("my-topic", msg);
        log.info("发送消息：{}", jsonObject.toJSONString());

    }
}
